package com.aspose.cells;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;

class zap extends ze {
   private static boolean u = true;
   private static boolean v = true;
   private zbfn w;
   private Graphics2D x;
   private BufferedImage y;
   private ImageFormat z;

   public zap(int var1, int var2, ImageFormat var3, ImageOrPrintOptions var4, com.aspose.cells.b.a.d.zm var5, zbfn var6) {
      super(var1, var2, var3, var4, var5, var6);
      this.w = var6;
      if (var6 != null) {
         this.w.a(this);
      }

      this.z = var3;
      this.o();
   }

   private void o() {
      int var1 = this.a(this.m);
      this.y = new BufferedImage(this.p(), this.q(), var1);
      this.x = this.y.createGraphics();
      if (this.n != null && this.n.g.size() > 0) {
         this.x.setRenderingHints(this.n.g);
         if (this.n.g.containsKey(RenderingHints.KEY_ANTIALIASING)) {
            if (!this.n.g.containsValue(RenderingHints.VALUE_ANTIALIAS_ON) && !this.n.g.containsValue(RenderingHints.VALUE_ANTIALIAS_DEFAULT)) {
               if (this.n.g.containsValue(RenderingHints.VALUE_ANTIALIAS_OFF)) {
                  this.a(3);
               }
            } else {
               this.a(4);
            }
         }

         if (this.n.g.containsKey(RenderingHints.KEY_TEXT_ANTIALIASING) && (this.n.g.containsValue(RenderingHints.VALUE_TEXT_ANTIALIAS_ON) || this.n.g.containsValue(RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT))) {
            this.b(4);
         }
      }

   }

   private int p() {
      return this.n == null ? this.k : (int)Math.ceil((double)((float)(this.k * this.n.getHorizontalResolution()) / 96.0F));
   }

   private int q() {
      return this.n == null ? this.l : (int)Math.ceil((double)((float)(this.l * this.n.getVerticalResolution()) / 96.0F));
   }

   public com.aspose.cells.b.a.b.za m() throws Exception {
      if (this.m.equals(ImageFormat.getEmf())) {
         return null;
      } else {
         if (this.y == null) {
            int var1 = this.a(this.m);
            this.y = new BufferedImage(this.p(), this.q(), var1);
            this.x = this.y.createGraphics();
         }

         com.aspose.cells.a.d.zm var3 = this.r();
         com.aspose.cells.a.d.zat var2 = new com.aspose.cells.a.d.zat();
         var2.a(var3, this.x, 0.0F, 0.0F, 1.0F);
         return new com.aspose.cells.b.a.b.za(this.y, this.z);
      }
   }

   public void a(com.aspose.cells.b.a.b.zj var1, com.aspose.cells.b.a.b.zp var2, int var3, int var4, int var5, int var6, int var7) {
      if (var1 != null) {
         if (var1 instanceof com.aspose.cells.b.a.b.za) {
            BufferedImage var8 = var1.f();
            if (var8 == null) {
               return;
            }

            var3 = var3 < 0 ? 0 : var3;
            var4 = var4 < 0 ? 0 : var4;
            var5 = var5 > var8.getWidth() ? var8.getWidth() : var5;
            var6 = var6 > var8.getHeight() ? var8.getHeight() : var6;
            if (var6 > var8.getHeight() - var4) {
               var6 = var8.getHeight() - var4;
            }

            BufferedImage var9 = new BufferedImage(var5, var6, var8.getType());
            Raster var10 = var8.getData(new Rectangle(var3, var4, var5, var6)).createTranslatedChild(0, 0);
            var9.setData(var10);
            this.a(new com.aspose.cells.b.a.b.za(var9, var1.e()), var2.c(), var2.d(), var2.e(), var2.f());
         }

         if (var1 instanceof com.aspose.cells.b.a.b.b.zf) {
            this.a((float)var2.c(), (float)var2.d(), (float)var2.e(), (float)var2.f(), var1);
         }

      }
   }

   protected void b(com.aspose.cells.b.a.b.zr var1, int var2) {
      com.aspose.cells.b.a.b.zr var3;
      switch(var2) {
      case 0:
         var3 = var1;
         break;
      default:
         var3 = var1;
      }

      this.b.a(var3);
      com.aspose.cells.a.d.zn var4 = zadc.a((com.aspose.cells.b.a.b.zr)var3, this);
      var4.a(this.b.b().b());
      com.aspose.cells.a.d.zb var5 = new com.aspose.cells.a.d.zb();
      var5.a(var4);
      this.f.a((com.aspose.cells.a.d.zl)var5);
      this.f = var5;
      this.a(this.b.d());
      this.b(this.b.e());
   }

   protected void a(String var1, com.aspose.cells.b.a.b.zg var2, com.aspose.cells.b.a.b.zc var3, float var4, float var5, float var6, float var7, com.aspose.cells.b.a.b.zv var8) {
      if (var3 == null) {
         throw new IllegalArgumentException("brush");
      } else if (var2 == null) {
         throw new IllegalArgumentException("font");
      } else {
         boolean var9 = false;
         if (var8 != null) {
            var9 = (var8.d() & 2) != 0;
         }

         if (var9) {
            throw new IllegalArgumentException("DrawString in Vertical not supported.");
         } else {
            this.b(var1, var2, var3, var4, var5, var6, var7, var8);
         }
      }
   }

   private void b(String var1, com.aspose.cells.b.a.b.zg var2, com.aspose.cells.b.a.b.zc var3, float var4, float var5, float var6, float var7, com.aspose.cells.b.a.b.zv var8) {
      try {
         String var9 = var2.b().b();
         if (!com.aspose.cells.a.d.zfs.a(var9, var2.k(), var1)) {
            var9 = com.aspose.cells.a.d.zbw.a;
            if (!com.aspose.cells.a.d.zfs.a(var9, var2.k(), var1)) {
               com.aspose.cells.a.d.zfq var10 = com.aspose.cells.a.d.zfs.a(var2.k(), var1);
               if (var10 != null) {
                  var9 = var10.b();
                  if (var9.equals("Symbol")) {
                     StringBuilder var11 = new StringBuilder(var1.length());
                     int var12 = 0;

                     while(true) {
                        if (var12 >= var1.length()) {
                           var1 = var11.toString();
                           break;
                        }

                        char var13 = var1.charAt(var12);
                        if (var13 >= '\uf020' && var13 <= '\uf0ff') {
                           var13 = (char)(var13 & 255);
                        }

                        var11.append(var13);
                        ++var12;
                     }
                  }
               }
            }
         }

         com.aspose.cells.a.d.zav var22 = new com.aspose.cells.a.d.zav(var9, this.a(var2), var2.k());
         int var23 = 0;
         com.aspose.cells.b.a.b.zt var24 = this.a(var1, var2, new com.aspose.cells.b.a.b.zt(var6, var7), var8);
         float var25 = 0.0F;
         if (var8 != null && var8.b() != 0) {
            if (var8.b() == 1) {
               var25 = (var7 - var24.c()) / 2.0F;
            } else if (var8.b() == 2) {
               var25 = var7 - var24.c();
            }
         }

         var5 = var25 > 0.0F ? var5 + var25 : var5;
         float var14 = 0.0F;
         com.aspose.cells.b.a.b.d.a.zd var15 = new com.aspose.cells.b.a.b.d.a.zd(var1, var2, var8, var6, var7, this.x);
         int var16 = 0;

         for(com.aspose.cells.b.a.b.d.a.zc var17 = var15.m(); var17 != null; var17 = var15.m()) {
            ++var16;
            int var18 = var17.c();
            String var19 = var1.substring(var23, var23 + var18);
            var19 = var19.replace("\r", "");
            var19 = var19.replace("\n", "");
            var23 += var18;
            float var20 = var17.d() + var17.e();
            if (var16 == 1) {
               var20 += var17.f();
            }

            var20 = var15.b(var20);
            var14 += var20;
            if (var8 != null && var8.h() && var14 > var7) {
               break;
            }

            if (var16 == 1) {
               this.a(var19, var22, var3, var4 + var17.h(), var5 + var17.f());
            } else {
               this.a(var19, var22, var3, var4 + var17.h(), var5);
            }

            var5 += var20;
         }
      } catch (Exception var21) {
         com.aspose.cells.a.e.zf.a((Throwable)var21);
      }

   }

   protected void a(String var1, com.aspose.cells.a.d.zav var2, com.aspose.cells.b.a.b.zc var3, float var4, float var5) {
      com.aspose.cells.a.d.zf var6 = new com.aspose.cells.a.d.zf();
      var6.a(this.b.b().b());
      var6.a(var1);
      var6.a(var2);
      if (var3 instanceof com.aspose.cells.b.a.b.zu) {
         var6.a(Color.fromArgb(((com.aspose.cells.b.a.b.zu)var3).a().toArgb()));
      } else {
         var6.a(Color.getBlack());
      }

      int var7 = com.aspose.cells.a.d.zaj.b((double)var6.d().h(), (double)this.h());
      var6.a(new com.aspose.cells.b.a.b.zo(this.b(var4), this.b(var5 + (float)var7)));
      if (var2.g()) {
         var6.c = 1;
      }

      this.f.a((com.aspose.cells.a.d.zl)var6);
   }

   protected void a(float var1, float var2, float var3, float var4, com.aspose.cells.b.a.b.zj var5) {
      if (var5 != null) {
         byte[] var6 = null;

         try {
            com.aspose.cells.a.d.zj var11;
            com.aspose.cells.a.d.zb var12;
            if (var5 instanceof com.aspose.cells.b.a.b.za) {
               String var7 = ImageFormat.a(var5.e());
               if (var7 != null) {
                  ByteArrayOutputStream var8 = new ByteArrayOutputStream();
                  ImageIO.write(var5.f(), var7, var8);
                  var6 = var8.toByteArray();
               }
            } else if (var5 instanceof com.aspose.cells.b.a.b.b.zf) {
               var11 = new com.aspose.cells.a.d.zj(new com.aspose.cells.b.a.b.zo(this.b(var1), this.b(var2)), new com.aspose.cells.b.a.b.zt(this.b(var3), this.b(var4)), var5.f);
               var12 = com.aspose.cells.a.d.zch.a(var11);
               com.aspose.cells.a.d.zb var9 = new com.aspose.cells.a.d.zb();
               var9.a(this.b.b().b());
               var9.a((com.aspose.cells.a.d.zl)var12);
               this.f.a((com.aspose.cells.a.d.zl)var9);
            }

            if (var6 != null) {
               var11 = new com.aspose.cells.a.d.zj(new com.aspose.cells.b.a.b.zo(this.b(var1), this.b(var2)), new com.aspose.cells.b.a.b.zt(this.b(var3), this.b(var4)), var6);
               var12 = new com.aspose.cells.a.d.zb();
               var12.a(this.b.b().b());
               var12.a((com.aspose.cells.a.d.zl)var11);
               this.f.a((com.aspose.cells.a.d.zl)var12);
            }
         } catch (Exception var10) {
            com.aspose.cells.a.c.zl.b(var10);
         }

      }
   }

   public float c(float var1, float var2) {
      return (float)com.aspose.cells.a.d.zaj.c((double)var1, (double)var2);
   }

   public float b(float var1) {
      return this.c(var1, this.g());
   }

   public void l() throws Exception {
      if (this.y == null) {
         int var1 = this.a(this.m);
         this.y = new BufferedImage(this.p(), this.q(), var1);
         this.x = this.y.createGraphics();
      }

      com.aspose.cells.a.d.zm var8 = this.r();
      float var2 = this.n == null ? 1.0F : (float)this.n.getHorizontalResolution() / 96.0F;
      float var3 = this.n == null ? 1.0F : (float)this.n.getVerticalResolution() / 96.0F;
      if (this.m.equals(ImageFormat.getEmf())) {
         zam var4 = new zam();
         var4.a(var8, this.o.j(), 0.0F, 0.0F, var2, var3);
      } else {
         String var9 = this.b(this.m);
         if (var9 == null) {
            throw new Exception(" Image format(" + this.m.getName() + ") is not supported.");
         }

         com.aspose.cells.a.d.zat var5 = new com.aspose.cells.a.d.zat();
         var5.a(var8, this.x, 0.0F, 0.0F, var2, var3);
         OutputStream var6 = this.o.j();
         String var7 = var9.toUpperCase();
         if (!var7.equals("JPG") && !var7.equals("JPEG")) {
            ImageIO.write(this.y, var9, var6);
            var6.flush();
         } else {
            com.aspose.cells.a.d.zbo.a(this.y, this.n, var6);
         }
      }

   }

   public com.aspose.cells.a.d.zb n() throws Exception {
      if (this.w != null) {
         this.w.ao();
      }

      return this.e;
   }

   private com.aspose.cells.a.d.zm r() throws Exception {
      if (this.w != null) {
         this.w.ao();
      }

      float var1 = (float)com.aspose.cells.a.d.zaj.c((double)this.k, (double)this.g());
      float var2 = (float)com.aspose.cells.a.d.zaj.c((double)this.l, (double)this.h());
      com.aspose.cells.a.d.zm var3 = new com.aspose.cells.a.d.zm(new com.aspose.cells.b.a.b.zt(var1, var2));
      var3.a(this.e);
      return var3;
   }

   private float a(com.aspose.cells.b.a.b.zg var1) {
      float var2 = 10.0F;
      if (var1.m() == 3) {
         var2 = var1.h();
      } else {
         if (var1.m() != 2 && var1.m() != 1) {
            throw new UnsupportedOperationException();
         }

         var2 = (float)com.aspose.cells.a.d.zaj.c((double)var1.h(), (double)this.g());
      }

      return var2 * (float)this.a;
   }

   private int a(ImageFormat var1) {
      return this.m.equals(ImageFormat.getPng()) ? 2 : 5;
   }

   private String b(ImageFormat var1) {
      return ImageFormat.a(var1);
   }

   public void a(int var1) {
      super.a(var1);
      if (this.f != null) {
         switch(var1) {
         case -1:
         default:
            break;
         case 0:
         case 3:
            this.f.a(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            this.f.a(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT);
            break;
         case 1:
            this.f.a(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            this.f.a(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
            break;
         case 2:
         case 4:
            this.f.a(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            this.f.a(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
         }
      }

   }

   public void b(int var1) {
      super.b(var1);
      if (this.f != null) {
         switch(var1) {
         case 0:
         case 1:
         case 2:
         case 3:
         case 5:
            this.f.a(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
            break;
         case 4:
            this.f.a(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
         }
      }

   }
}
